package com.banshuibao.bsb_android.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.banshuibao.bsb_android.Constants;
import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.adapter.InformationAdapter;
import com.banshuibao.bsb_android.http.HttpResponse;
import com.banshuibao.bsb_android.http.HttpUtil;
import com.banshuibao.bsb_android.model.SystemInforModel;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshBase;
import com.banshuibao.bsb_android.pulltolistview.PullToRefreshListView;
import com.banshuibao.bsb_android.util.IntentManager;

import java.util.List;

public class SystemInformationActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener2 {

    private PullToRefreshListView pullToRefreshListView;
    private ListView listView;
    private InformationAdapter adapter;
    private int startId, endId, rows;
    private boolean isUp = false;
    private SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_information);
        editor = getSharedPreferences("isRead", MODE_PRIVATE).edit();
        initView();
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestNet();
    }

    public void initView() {
        setTitleText("消息通知");
        pullToRefreshListView = findViewById(R.id.ptrlv);
        pullToRefreshListView.setMode(PullToRefreshBase.Mode.DISABLED);
        pullToRefreshListView.setOnRefreshListener(this);
        listView = pullToRefreshListView.getRefreshableView();
        adapter = new InformationAdapter(this);
        listView.setDivider(null);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               if(adapter != null){
                   SystemInforModel.DataBean dataBean =  adapter.getItem(position-1);
                   editor.putBoolean(String.valueOf(dataBean.getId()),true).commit();
                   IntentManager.goSystemInformationDetailsActivity(context,dataBean.getId());
               }
            }
        });
    }

    public void setData() {
        isUp = false;
        startId = 1;
        endId = startId + 9;
        if (adapter != null && adapter.getCount() > 10) {
            endId = adapter.getCount();
        }
        requestNet();
    }

    public void requestNet() {

        HttpUtil.doPost(Constants.Url.INFORMATION_LIST, null, new HttpResponse(this, SystemInforModel.class) {
            @Override
            public void onResult(Object obj) {
                SystemInforModel inforModel = (SystemInforModel) obj;
                List<SystemInforModel.DataBean> beanList = inforModel.getData();
                adapter.setDownData(beanList);
            }
        });
    }


    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        setData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        isUp = true;
        endId = startId + 9;
        requestNet();
    }


}
